package com.example.wangzh.mytest.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;

import com.example.wangzh.mytest.R;

import java.util.Calendar;

//该控件的代码完全由deepseek生成
public class ClockView extends View {

    // 默认颜色配置
    private int hourHandColor = Color.BLACK;
    private int minuteHandColor = Color.BLACK;
    private int secondHandColor = Color.RED;
    private int backgroundColor = Color.WHITE;

    // 样式控制
    private boolean showRomanNumerals = false;
    private boolean showMinuteMarks = true;
    private int dialStyle = 0; // 0-modern, 1-classic, 2-minimal

    // 绘制工具
    private Paint paint;
    private Paint textPaint;
    private Paint centerDotPaint;
    private Paint hourMarkPaint;

    // 尺寸参数
    private int width, height;
    private int centerX, centerY;
    private int radius;

    // 时间处理
    private Handler handler;
    private Runnable runnable;

    public ClockView(Context context) {
        super(context);
        init(null);
    }

    public ClockView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public ClockView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        // 读取自定义属性
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ClockView);
            showRomanNumerals = a.getBoolean(R.styleable.ClockView_showRomanNumerals, false);
            showMinuteMarks = a.getBoolean(R.styleable.ClockView_showMinuteMarks, true);
            dialStyle = a.getInt(R.styleable.ClockView_dialStyle, 0);
            hourHandColor = a.getColor(R.styleable.ClockView_hourHandColor, Color.BLACK);
            minuteHandColor = a.getColor(R.styleable.ClockView_minuteHandColor, Color.BLACK);
            secondHandColor = a.getColor(R.styleable.ClockView_secondHandColor, Color.RED);
            backgroundColor = a.getColor(R.styleable.ClockView_backgroundColor, Color.WHITE);
            a.recycle();
        }

        // 基础画笔配置
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);

        // 文字画笔
        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setTextSize(40);
        textPaint.setTextAlign(Paint.Align.CENTER);

        // 中心点画笔
        centerDotPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        centerDotPaint.setStyle(Paint.Style.FILL);

        // 小时刻度画笔
        hourMarkPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        hourMarkPaint.setStrokeWidth(8);

        // 时间更新
        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {
                invalidate();
                handler.postDelayed(this, 1000);
            }
        };
        handler.post(runnable);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
        centerX = w / 2;
        centerY = h / 2;
        radius = Math.min(w, h) / 2 - 20;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        applyStyleSettings();
        drawBackground(canvas);
        drawFace(canvas);
        drawNumbers(canvas);
        drawCenter(canvas);
        drawHands(canvas);
    }

    private void applyStyleSettings() {
        switch (dialStyle) {
            case 1: // 古典风格
                paint.setColor(Color.parseColor("#8B4513"));
                textPaint.setColor(Color.YELLOW);
                hourMarkPaint.setColor(Color.parseColor("#CD8500"));
                backgroundColor = Color.parseColor("#F5DEB3");
                break;
            case 2: // 极简风格
                paint.setColor(Color.WHITE);
                textPaint.setColor(Color.BLACK);
                hourMarkPaint.setColor(Color.DKGRAY);
                backgroundColor = Color.WHITE;
                break;
            default: // 现代风格
                paint.setColor(Color.BLACK);
                textPaint.setColor(Color.BLACK);
                hourMarkPaint.setColor(Color.DKGRAY);
        }
    }

    private void drawBackground(Canvas canvas) {
        Paint bgPaint = new Paint();
        bgPaint.setShader(new LinearGradient(0, 0, width, height,
                new int[]{Color.LTGRAY, backgroundColor, Color.LTGRAY},
                null, Shader.TileMode.MIRROR));
        canvas.drawCircle(centerX, centerY, radius + 20, bgPaint);
    }

    private void drawFace(Canvas canvas) {
        // 外层白圈
        paint.setColor(Color.WHITE);
        canvas.drawCircle(centerX, centerY, radius + 10, paint);

        // 内层主表盘
        paint.setColor(Color.BLACK);
        canvas.drawCircle(centerX, centerY, radius, paint);

        // 分钟刻度
        if (showMinuteMarks) {
            Paint minuteMarkPaint = new Paint();
            minuteMarkPaint.setColor(Color.LTGRAY);
            minuteMarkPaint.setStrokeWidth(2);
            for (int i = 0; i < 60; i++) {
                if (i % 5 != 0) {
                    double angle = Math.PI / 30 * i;
                    drawMark(canvas, angle, radius - 15, radius, minuteMarkPaint);
                }
            }
        }

        // 小时刻度
        for (int i = 0; i < 12; i++) {
            double angle = Math.PI / 6 * i;
            if (i % 3 == 0) {
                drawMark(canvas, angle, radius - 40, radius - 20, hourMarkPaint);
            } else {
                drawMark(canvas, angle, radius - 30, radius - 15, hourMarkPaint);
            }
        }
    }

    private void drawMark(Canvas canvas, double angle, float startOffset, float endOffset, Paint paint) {
        float startX = (float) (centerX + startOffset * Math.sin(angle));
        float startY = (float) (centerY - startOffset * Math.cos(angle));
        float endX = (float) (centerX + endOffset * Math.sin(angle));
        float endY = (float) (centerY - endOffset * Math.cos(angle));
        canvas.drawLine(startX, startY, endX, endY, paint);
    }

    private void drawNumbers(Canvas canvas) {
        String[] numbers = showRomanNumerals ?
                new String[]{"XII","I","II","III","IV","V","VI","VII","VIII","IX","X","XI"} :
                new String[]{"12","1","2","3","4","5","6","7","8","9","10","11"};

        for (int i = 1; i < 12; i++) {
            double angle = Math.PI / 6 * i;
            float textX = (float) (centerX + (radius - 70) * Math.sin(angle));
            float textY = (float) (centerY - (radius - 70) * Math.cos(angle)) + 15;
            canvas.drawText(numbers[i], textX, textY, textPaint);
        }
        canvas.drawText(numbers[0], centerX, centerY - radius + 80, textPaint);
    }

    private void drawCenter(Canvas canvas) {
        centerDotPaint.setColor(secondHandColor);
        canvas.drawCircle(centerX, centerY, 10, centerDotPaint);
        centerDotPaint.setColor(Color.WHITE);
        canvas.drawCircle(centerX, centerY, 5, centerDotPaint);
    }

    private void drawHands(Canvas canvas) {
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);

        // 时针
        float hourAngle = (hour + minute / 60.0f) * 30;
        drawHand(canvas, hourAngle, radius * 0.5f, hourHandColor, 8);

        // 分针
        float minuteAngle = (minute + second / 60.0f) * 6;
        drawHand(canvas, minuteAngle, radius * 0.7f, minuteHandColor, 5);

        // 秒针（带尾部装饰）
        float secondAngle = second * 6;
        drawHand(canvas, secondAngle, radius * 0.9f, secondHandColor, 3);
        drawTail(canvas, secondAngle, radius * 0.2f);
    }

    private void drawHand(Canvas canvas, float angle, float length, int color, int width) {
        Paint handPaint = new Paint();
        handPaint.setColor(color);
        handPaint.setStrokeWidth(width);
        handPaint.setAntiAlias(true);

        float endX = (float) (centerX + length * Math.sin(Math.toRadians(angle)));
        float endY = (float) (centerY - length * Math.cos(Math.toRadians(angle)));
        canvas.drawLine(centerX, centerY, endX, endY, handPaint);
    }

    private void drawTail(Canvas canvas, float angle, float length) {
        Paint tailPaint = new Paint();
        tailPaint.setColor(secondHandColor);
        tailPaint.setStrokeWidth(3);

        float endX = (float) (centerX - length * Math.sin(Math.toRadians(angle)));
        float endY = (float) (centerY + length * Math.cos(Math.toRadians(angle)));
        canvas.drawLine(centerX, centerY, endX, endY, tailPaint);
    }
}